package com.yh.blog.serviceImpl;

import com.yh.blog.config.PersonConfig;
import com.yh.blog.domain.Blog;
import com.yh.blog.domain.Types;
import com.yh.blog.mapper.BlogMapper;
import com.yh.blog.mapper.TypesMapper;
import com.yh.blog.service.TypesService;
import com.yh.blog.util.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TypesServicesImpl implements TypesService {
    @Autowired
    private PersonConfig personConfig;

    @Resource
    private TypesMapper typesMapper;

    @Resource
    private BlogMapper blogMapper;

    @Override
    public boolean append(Types type) {
        return this.typesMapper.insert(type);
    }

    @Override
    public boolean modifyTypeName(Integer typesId,String typesName) {
        Types type = new Types();
        type.setTypesId(typesId);
        type.setTypesName(typesName);
        return this.typesMapper.update(type);
    }

    @Override
    public boolean remove(Integer typesId) {
        return this.typesMapper.delete(typesId);
    }

    @Override
    public Types detail(Integer typesId) {
        return this.typesMapper.selectById(typesId);
    }

    @Override
    public Pager<Types> list(Integer page) {
        List<Types> datas=this.typesMapper.list((page-1)*personConfig.getPage_size(),personConfig.getPage_size());
        int size=this.typesMapper.size();
        return new Pager<>(datas, size, page, personConfig.getPage_size());
    }

    @Override
    public List<Types> list() {
        List<Types> types=this.typesMapper.list(0, 0);
        for (Types type : types) {
            Blog condition=new Blog();
            condition.setTypesId(type.getTypesId());
            condition.setPublish(true);
            type.setBlogNums(this.blogMapper.sizeByCondition(condition));
        }
        return types;
    }

//    @Override
//    public boolean typesNameIsExists(String typesName) {
//        return this.typesMapper.selectByName(typesName);
//    }
}
